package com.atguigu.controller;

import com.atguigu.bean.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.ViewBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName: javaweb
 *
 * @author: 赵伟风
 * time: 2022/7/4 16:26 周一
 * description: 用户关键字查询的controller
 */
@WebServlet("/user/search")
public class UserSearchController extends ViewBaseServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //2.获取请求参数
        String name = req.getParameter("name");

        //3.调用业务逻辑代码
        UserService userService = new UserServiceImpl();
        List<User> userList = userService.search(name);

        //4.响应前端数据
        //数据存储到共享域
        //存储数据的key必须和 userShow相同
        req.setAttribute("userList",userList);
        //跳转到userList.html
        processTemplate("userList",req,resp);
    }

}
